package cn.nuc.controller.sensorfire.back;

import cn.nuc.pojo.entity.ResponseData;
import cn.nuc.pojo.sensorfire.Sensor;
import cn.nuc.service.sensorfire.ISensorService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/system/sensor/back")
@Api(tags = "传感器后台管理")
public class SensorController {
    @Autowired
    private ISensorService sensorService;
    @GetMapping("/list")
    @ApiOperation(value = "查看所有传感器信息")
    public ResponseData<Sensor> getAll(){
        List<Sensor> sensorList = sensorService.list();
        if(sensorList != null){
            return ResponseData.success().putDataVule("sensorList" , sensorList);
        }
        return ResponseData.serverInternalError();
    }
    @PutMapping("/saveOrUpdate")
    @ApiOperation(value = "添加或更新传感器信息")
    @ApiImplicitParam(value = "要添加或更新的传感器对象" , name = "sensor" , required = true , dataType = "Sensor" , paramType = "body")
    public ResponseData<Sensor> saveOrUpdate(@RequestBody Sensor sensor){
//        核实端口是否冲突
        List<Sensor> list = sensorService.list(new QueryWrapper<Sensor>().eq("ip", sensor.getIp())
                .eq("port", sensor.getPort()));
        if(list.size() != 0){
            return ResponseData.cusstomerError("此IP和端口已有传感器占用");
        }
//        不冲突则加入新的传感器或者更新信息
        boolean saveOrUpdate = sensorService.saveOrUpdate(sensor);
        if(saveOrUpdate){
            return ResponseData.success().putDataVule("saveOrUpdate" , true);
        }
        return ResponseData.serverInternalError();
    }
//    模糊查询传感器
    @GetMapping("/listByCondition")
    @ApiOperation(value = "跟据条件查询传感器")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "传感器位置" , name = "location" , required = false , dataType = "String" , paramType = "query"),
            @ApiImplicitParam(value = "传感器状态" , name = "status" , required = false , dataType = "String" , paramType = "query")
    })
    public ResponseData<Sensor> getSensorByCondition(String location){
        List<Sensor> sensorList = null;
        if(location != null  && !StringUtils.isEmpty(location) ){
            sensorList = sensorService.list(new QueryWrapper<Sensor>()
                    .like("location", location));
        }
        if(StringUtils.isEmpty(location)  || location == null){
            sensorList = sensorService.list();
        }
        return ResponseData.success().putDataVule("sensorList" , sensorList);
    }
    @DeleteMapping("/deleteById")
    @ApiOperation(value = "通过id删除传感器")
    @ApiImplicitParam(value = "要删除的传感器id" , name = "id" , required = true , dataType = "Integer" , paramType = "query")
    public ResponseData<Sensor> deleteSensor(Integer id){
        boolean remove = sensorService.removeById(id);
        if(remove){
            return ResponseData.success().putDataVule("remove" , true);
        }
        return ResponseData.serverInternalError();
    }
    @GetMapping("/getOne")
    public ResponseData<Sensor> getOneById(Integer id){
        List<Sensor> sensors = sensorService.list(new QueryWrapper<Sensor>().eq("id", id));
        if(sensors != null){
            return ResponseData.success().putDataVule("sensor" , sensors.get(0));
        }
        return ResponseData.serverInternalError();
    }
}
